WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
time_stamp.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// wolf includes
22#include "core/common/wolf.h"
23
24// C, std
25#include <sys/time.h>
26#include <iostream>
27#include <iomanip>
28
29static const unsigned int NANOSECS = 1000000000;
30
31namespace wolf
32{
39{
40 protected:
41 unsigned long int time_stamp_nano_;
42 bool is_valid_; // time stamp has a valid value
43
44 public:
50 TimeStamp();
51
57 TimeStamp(const TimeStamp& _ts);
58
64 TimeStamp(const double& _ts);
65
71 TimeStamp(const unsigned long int& _sec, const unsigned long int& _nsec);
72
78 ~TimeStamp();
79
83 static TimeStamp Invalid();
84 bool ok() const;
85 void setOk();
86 void setNOk();
87
88 static TimeStamp Now();
89
92 void setToNow();
93
98 void set(const timeval& ts);
99
105 void set(const unsigned long int& sec, const unsigned long int& nanosec);
106
112 void set(const double& ts);
113
119 double get() const;
120
126 unsigned long int getSeconds() const;
127
133 unsigned long int getNanoSeconds() const;
134
140 void operator=(const double& ts);
141
147 void operator=(const TimeStamp& ts);
148
154 bool operator!=(const TimeStamp& ts) const;
155 bool operator==(const TimeStamp& ts) const;
156 bool operator<(const TimeStamp& ts) const;
157 bool operator>(const TimeStamp& ts) const;
158
164 bool operator<=(const TimeStamp& ts) const;
165 bool operator>=(const TimeStamp& ts) const;
166
169 void operator+=(const double& dt);
170
173 TimeStamp operator+(const double& dt) const;
174
177 void operator-=(const double& dt);
178
184 TimeStamp operator-(const double& ts) const;
185
191 double operator-(const TimeStamp& ts) const;
192
198 void print(std::ostream& ost = std::cout) const;
199
200 friend std::ostream& operator<<(std::ostream& os, const TimeStamp& _ts);
201
206 std::string string() const;
207};
208
210{
211 return TimeStamp(-1.0);
212}
213
214inline bool TimeStamp::ok() const
215{
216 return is_valid_;
217}
218
219inline void TimeStamp::setOk()
220{
221 is_valid_ = true;
222}
223
224inline void TimeStamp::setNOk()
225{
226 is_valid_ = false;
227}
228
229inline void TimeStamp::set(const double& ts)
230{
231 time_stamp_nano_ = (ts >= 0 ? (unsigned long int)(ts * NANOSECS) : 0);
232 is_valid_ = (ts >= 0);
233}
234
235inline void TimeStamp::set(const unsigned long int& sec, const unsigned long int& nanosec)
236{
237 time_stamp_nano_ = sec * NANOSECS + nanosec;
238 is_valid_ = true;
239}
240
241inline void TimeStamp::set(const timeval& ts)
242{
243 time_stamp_nano_ = (unsigned long int)(ts.tv_sec * NANOSECS) + (unsigned long int)(ts.tv_usec * 1000);
244 is_valid_ = (ts.tv_sec >= 0 and ts.tv_usec >= 0);
245}
246
247inline double TimeStamp::get() const
248{
249 return ((double)(time_stamp_nano_)) * 1e-9;
250}
251
252inline unsigned long int TimeStamp::getSeconds() const
253{
254 return time_stamp_nano_ / NANOSECS;
255}
256
257inline unsigned long int TimeStamp::getNanoSeconds() const
258{
259 return time_stamp_nano_ % NANOSECS;
260}
261
262inline void TimeStamp::operator=(const TimeStamp& ts)
263{
265 is_valid_ = ts.is_valid_;
266}
267
268inline void TimeStamp::operator=(const double& ts)
269{
270 time_stamp_nano_ = (unsigned long int)(ts * NANOSECS);
271 is_valid_ = (ts >= 0);
272}
273
274inline bool TimeStamp::operator==(const TimeStamp& ts) const
275{
276 return (time_stamp_nano_ == ts.time_stamp_nano_);
277}
278
279inline bool TimeStamp::operator!=(const TimeStamp& ts) const
280{
281 return (time_stamp_nano_ != ts.time_stamp_nano_);
282}
283
284inline bool TimeStamp::operator<(const TimeStamp& ts) const
285{
287}
288
289inline bool TimeStamp::operator>(const TimeStamp& ts) const
290{
292}
293
294inline bool TimeStamp::operator<=(const TimeStamp& ts) const
295{
296 return (time_stamp_nano_ <= ts.time_stamp_nano_);
297}
298
299inline bool TimeStamp::operator>=(const TimeStamp& ts) const
300{
301 return (time_stamp_nano_ >= ts.time_stamp_nano_);
302}
303
304inline void TimeStamp::operator+=(const double& dt)
305{
306 time_stamp_nano_ += (unsigned long int)(dt * NANOSECS);
307}
308
309inline double TimeStamp::operator-(const TimeStamp& ts) const
310{
311 return double((long int)(time_stamp_nano_ - ts.time_stamp_nano_)) *
312 1e-9; // long int cast fix overflow in case of negative substraction result
313}
314
315inline std::string TimeStamp::string() const
316{
317 std::stringstream ss;
318 ss << *this;
319 return ss.str();
320}
321
322} // namespace wolf
TimeStamp implements basic functionalities for time stamps.
Definition: time_stamp.h:39
friend std::ostream & operator<<(std::ostream &os, const TimeStamp &_ts)
Definition: time_stamp.cpp:30
void operator-=(const double &dt)
Substraction-assign operator given a scalar_t (seconds)
Definition: time_stamp.cpp:91
static TimeStamp Now()
Definition: time_stamp.cpp:23
void set(const timeval &ts)
Set time stamp.
Definition: time_stamp.h:241
static TimeStamp Invalid()
Value of time stamp is valid.
Definition: time_stamp.h:209
bool operator<=(const TimeStamp &ts) const
comparison operator
Definition: time_stamp.h:294
void operator=(const double &ts)
Assignement operator.
Definition: time_stamp.h:268
bool operator>=(const TimeStamp &ts) const
Definition: time_stamp.h:299
bool operator>(const TimeStamp &ts) const
Definition: time_stamp.h:289
bool operator<(const TimeStamp &ts) const
Definition: time_stamp.h:284
TimeStamp operator-(const double &ts) const
difference operator
Definition: time_stamp.cpp:84
TimeStamp()
Constructor.
Definition: time_stamp.cpp:39
void setNOk()
Definition: time_stamp.h:224
double get() const
Get time stamp.
Definition: time_stamp.h:247
bool operator!=(const TimeStamp &ts) const
comparison operator
Definition: time_stamp.h:279
bool is_valid_
Definition: time_stamp.h:42
unsigned long int getSeconds() const
Get time stamp (only seconds)
Definition: time_stamp.h:252
bool ok() const
Definition: time_stamp.h:214
~TimeStamp()
Destructor.
Definition: time_stamp.cpp:64
unsigned long int time_stamp_nano_
Time stamp. Expressed in nanoseconds from 1th jan 1970.
Definition: time_stamp.h:41
void operator+=(const double &dt)
Add-assign operator given a scalar_t (seconds)
Definition: time_stamp.h:304
TimeStamp operator+(const double &dt) const
Add-assign operator given a scalar_t (seconds)
Definition: time_stamp.cpp:77
void print(std::ostream &ost=std::cout) const
Prints time stamp to a given ostream.
Definition: time_stamp.cpp:98
bool operator==(const TimeStamp &ts) const
Definition: time_stamp.h:274
std::string string() const
Returns a string with the Timestamp.
Definition: time_stamp.h:315
void setOk()
Definition: time_stamp.h:219
unsigned long int getNanoSeconds() const
Get time stamp (only nano seconds)
Definition: time_stamp.h:257
void setToNow()
Time stamp to now.
Definition: time_stamp.cpp:69
Definition: capture_base.h:23
static const unsigned int NANOSECS
Definition: time_stamp.h:29